﻿namespace DesignModeConsole.结构型.适配器模式;

/// <summary>
/// 老的记录日志方法
/// </summary>
public interface ILogTargetOld
{
    void Write();
}

/// <summary>
/// 日志适配器
/// </summary>
public abstract class LogAdapter
{
    public abstract void WriteLog();
}

//新的日志记录方式
public class DatabaseLog : LogAdapter
{
    public override void WriteLog()
    {
        Console.WriteLine("新的数据库日志记录方式");
    }
}

//新的日志记录方式
public class FileLog : LogAdapter
{
    public override void WriteLog()
    {
        Console.WriteLine("新的文本文件日志记录方式");
    }
}

#region 类适配器模式

public class DatabaseLogAdapter : DatabaseLog, ILogTargetOld
{
    public void Write()
    {
        WriteLog();
    }
}

public class FileLogAdapter : FileLog, ILogTargetOld
{
    public void Write()
    {
        WriteLog();
    }
}
#endregion

#region 对象适配器-组合的方式

public class LogAdapter2 : ILogTargetOld
{
    private readonly LogAdapter _adapter;

    public LogAdapter2(LogAdapter adapter)
    {
        _adapter = adapter;
    }

    public void Write()
    {
        _adapter.WriteLog();
    }
}

#endregion